package com.hulk.boot.web.initializer;

import java.util.concurrent.Executors;

import javax.servlet.Filter;

import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafView;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
import org.thymeleaf.templateresolver.TemplateResolver;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;

/**
 * @author 作者 cmt
 * @version 创建时间：2016年7月6日 下午4:50:19 类说明
 */
/*
@Configuration
@EnableWebMvc
*/
public class WebMvcConfig extends WebMvcConfigurerAdapter {


	/*@Bean
	public TemplateResolver templateResolver() {

		TemplateResolver templateResolver = new ServletContextTemplateResolver();
		templateResolver.setPrefix("/WEB-INF/templates");
		templateResolver.setSuffix(".html");
		templateResolver.setTemplateMode("HTML5");
		return templateResolver;
	}

	@Bean
	public SpringTemplateEngine springTemplateEngine() {

		SpringTemplateEngine templateEngine = new SpringTemplateEngine();
		templateEngine.setTemplateResolver(templateResolver());
		return templateEngine;
	}

	@Bean
	public ThymeleafViewResolver thymeleafViewResolver() {

		ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
		thymeleafViewResolver.setTemplateEngine(springTemplateEngine());
		thymeleafViewResolver.setViewClass(ThymeleafView.class);
		return thymeleafViewResolver;

	}

	@Bean
	public MessageSource messageSource() {
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
		messageSource.setBasename("classpath:messages/messages");
		messageSource.setCacheSeconds(3600);
		messageSource.setUseCodeAsDefaultMessage(true);
		messageSource.setDefaultEncoding("UTF-8");
		return messageSource;
	}*/

	@Bean(name={"hiddenHttpMethodFilter"})
	public Filter hiddenHttpMethodFilter() {
		HiddenHttpMethodFilter  hiddenHttpMethodFilter	= new HiddenHttpMethodFilter();
		return  hiddenHttpMethodFilter;
		//StandardTemplateModeHandlers
	}
	
	@Bean
	public FilterRegistrationBean hiddenHttpMethodFilterRegistration() {
	    FilterRegistrationBean registration = new FilterRegistrationBean();
	    registration.setFilter(hiddenHttpMethodFilter());
	    registration.setName("hiddenHttpMethodFilter");
	    registration.addUrlPatterns("/*");
	    //registration.addServletNames("dispatcher");
	    registration.setOrder(2);
	    return registration;
	} 

	
	@Bean(name={"characterEncodingFilter"})
    public Filter characterEncodingFilter() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        return characterEncodingFilter;
    }
	
	
	
	
	@Bean
	public FilterRegistrationBean characterEncodingFilterRegistration() {
	    FilterRegistrationBean registration = new FilterRegistrationBean();
	    registration.setFilter(characterEncodingFilter());
	    registration.setName("characterEncodingFilter");
	    registration.addUrlPatterns("/*");
	   /* registration.addServletNames("dispatcher");*/
	    registration.setOrder(3);
	    return registration;
	} 
	
	/*@Bean
    public ContextLoaderListener contextLoaderListener() {
		ContextLoaderListener contextLoaderListener = new ContextLoaderListener();
        return contextLoaderListener;
    }*/
	
	@Bean
	public EventBus eventBus() {
		return new AsyncEventBus(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));
	}

/*	@Bean
	public ObjectMapper nonNullObjectMapper() {
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		return objectMapper;
	}*/

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/mystatic/**").addResourceLocations("classpath:/static/");
//		registry.addResourceHandler("/mystatic/mywebjar/**").addResourceLocations("/webjarslocator/");
//		super.addResourceHandlers(registry);
	}

	@Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //配置跳转
       // registry.addViewController("/nav").setViewName("forward:/static/nav.html");
    }
	
	
	@Override
	public void configurePathMatch(PathMatchConfigurer configurer) {
		configurer.setUseSuffixPatternMatch(false);
	}

}
